<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\index\model\Item;
use app\index\model\Monster as MonsterModel;

/**
 * 佣兵
 */
class Monster extends Base
{

    use \app\index\controller\traits\User;
    use \app\index\controller\traits\Xml;
    use \app\index\controller\traits\Monster;

    /**
     * 表字段初始化
     */
    public function _initialize()
    {
        //基类授权
        //
        parent::_initialize();

        //道具表查询

        $this->monster_path  = "./static/xml/acc_dragon_monster.xml";
        $this->quality_path  = "./static/xml/acc_monster_quality.xml";
        $this->location_path = "./static/xml/acc_monster_location.xml";

        $this->monster  = $this->getMonsterArr($this->monster_path); //dump($this->monster); //die;
        $this->quality  = $this->getArrFromXml($this->quality_path, 'id', 'name'); //dump($this->quality); //die;
        $this->location = $this->getArrFromXml($this->location_path, 'id', 'name'); //dump($this->location); //die;

        $this->prop_path = "./static/xml/acc_dragon_prop.xml";
        $this->prop      = $this->getArrFromXml($this->prop_path, 'id', 'name'); //dump($this->prop); //die;

    }
    /**
     * 基本信息查询
     */
    public function queryBasicInfo()
    {

        $id       = array_key_exists("id", $_REQUEST) ? $_REQUEST['id'] : null;
        $nickname = array_key_exists("nickname", $_REQUEST) ? $_REQUEST['nickname'] : null;

        $id = $this->getUserId($id, $nickname);

        if (empty($id)) {
            _callback(array('success' => true, 'msg' => "提示:没有找到玩家信息", 'type' => "user-not-exist"));
        }

        //查询玩家佣兵
        $res = $this->queryMonsterById($id);

        if ($res) {
            $this->item = $this->transferItemToArr($id);

            //字段转换
            $r = $this->transMonsterById($res);

            $return['rows']    = $r;
            $return['total']   = count($r);
            $return['success'] = true;
        } else {
            $return['rows']    = array();
            $return['total']   = 0;
            $return['success'] = true;
        }

        _callback($return);
    }
    /**
     * 佣兵信息查询
     */
    public function queryMonsterById($id)
    {

        $query = MonsterModel::where("`characterid`='{$id}'")
            ->field("`monsterid`,`level`,`count`,`status`,`employ_count`")
            ->order("monsterid asc")
            ->select();
        if (!$query) {
            return [];
        }

        $res = [];

        foreach ($query as $data) {
            array_push($res, $data->getData());
        }
        return $res;
    }

    /**
     * 数据转换
     */
    public function transMonsterById($recs)
    {

        $monster  = $this->monster;
        $quality  = $this->quality;
        $location = $this->location;

        if (!empty($recs)) {

            foreach ($recs as $k => $v) {
                if (array_key_exists($v['monsterid'], $monster)) {

                    $rune = $this->hasRune($v['monsterid']);

                    //佣兵ID查符文
                    $recs[$k]['being']   = $rune['being'];
                    $recs[$k]['speed']   = $rune['speed'];
                    $recs[$k]['attack']  = $rune['attack'];
                    $recs[$k]['defense'] = $rune['defense'];

                    //佣兵ID转名称
                    $recs[$k]['monsterid'] = $monster[$v['monsterid']]['name'];

                    //佣兵品质转名称
                    $quality_id          = $monster[$v['monsterid']]['quality'];
                    $recs[$k]['quality'] = array_key_exists($quality_id, $quality) ? $quality[$quality_id] : $quality_id;

                    //佣兵位置转名称
                    $location_id          = $monster[$v['monsterid']]['location'];
                    $recs[$k]['location'] = array_key_exists($location_id, $location) ? $location[$location_id] : $location_id;

                    //佣兵怒气消耗
                    $recs[$k]['need_angry'] = $monster[$v['monsterid']]['need_angry'];
                } else {
                    $recs[$k]['monsterid']  = $v['monsterid'];
                    $recs[$k]['quality']    = null;
                    $recs[$k]['location']   = null;
                    $recs[$k]['need_angry'] = null;
                }
            }
        }
        return ($recs);
    }
    /**
     * 获取装备
     */
    public function getMonsterItemsById($id)
    {

        $model = new Item();

        $query = $model->field("`monsterid`,`cid`")
            ->where("`characterid`='{$id}' AND `monsterid` IS NOT NULL AND `monsterid` <>''")
            ->order("monsterid asc")
            ->select();
        if (!$query) {
            return [];
        }

        $res = [];

        foreach ($query as $data) {
            array_push($res, $data->getData());
        }
        return $res;
    }
    /**
     * 数组转换
     */
    public function transferItemToArr($id)
    {

        $rtn  = array();
        $prop = $this->prop;

        $res = $this->getMonsterItemsById($id);
        if (!$res) {
            return null;
        }

        foreach ($res as $key => $value) {
            $arr = explode(';', $value['monsterid']);

            foreach ($arr as $k => $v) {
                if (!empty($v)) {
                    $rtn[$v][$value['cid']] = array_key_exists($value['cid'], $prop) ? $prop[$value['cid']] : $value['cid'];
                }
            }
        }
        return $rtn;
    }
    /**
     * 数组转换
     */
    public function hasRune($monsterid)
    {

        $rtn['being']   = 0;
        $rtn['speed']   = 0;
        $rtn['attack']  = 0;
        $rtn['defense'] = 0;

        $items = $this->item;

        if (!empty($this->item) && array_key_exists($monsterid, $items)) {

            $search = $items[$monsterid];

            foreach ($search as $k => $v) {
                if (strpos($v, "血量") !== false) {
                    $rtn['being'] = 1;
                }

                if (strpos($v, "速度") !== false) {
                    $rtn['speed'] = 1;
                }

                if (strpos($v, "攻击") !== false) {
                    $rtn['attack'] = 1;
                }

                if (strpos($v, "护甲") !== false) {
                    $rtn['defense'] = 1;
                }

            }
        }
        return ($rtn);
    }
}
